//
//  MLASavePinBillVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/24.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLASavePinBillVC.h"
#import "MLABaseSearchView.h"
#import "MLASavePinBillCell.h"
#import "MLASavePinBillAddVC.h"
#import "MLASavePinBillModel.h"
#import "MLASavePinBillDetailVC.h"

@interface MLASavePinBillVC ()

@end

@implementation MLASavePinBillVC  {
    MLABaseSearchView *searchView;
    
    NSMutableArray<MLASavePinBillModel *> *modelsArr;
    NSString *keyword;

}


- (void)viewDidLoad {
    [super viewDidLoad];

    keyword = @"";
    
    [self configTitle:@"票据凭证"];
    [self configRightItemWithTitle:@"添加"];

    searchView = [[MLABaseSearchView alloc] init];
    [self.view addSubview:searchView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.titleView.mas_bottom);
        make.height.mas_equalTo(80);
    }];

    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.textColor = CX_GetColorTitle();
    titleLbl.font = CX_GetNormalFont(18);
    titleLbl.text = @"票据";
    [self.view addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(self->searchView.mas_bottom).offset(1);
    }];
    
    UILabel *subTitleLbl = [[UILabel alloc] init];
    subTitleLbl.textColor = CX_GetColorHex(0x999999, 1);
    subTitleLbl.font = CX_GetNormalFont(14);
    subTitleLbl.text = @"凭证";
    [self.view addSubview:subTitleLbl];
    [subTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLbl.mas_right).offset(18);
        make.bottom.equalTo(titleLbl);
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLbl.mas_bottom).offset(19);
        make.left.bottom.right.equalTo(self.view);
    }];
    self.tableView.rowHeight = 105.f;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MLASavePinBillCell class] forCellReuseIdentifier:kBillCellID];
 
    self.haveRefreshHeader = YES;
    [self loadData];

    [self addClickActions];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    NSDictionary *params = @{@"title":keyword};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:kBillList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLASavePinBillModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page),
                             @"title":keyword};
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:kBillList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->modelsArr addObjectsFromArray:[MLASavePinBillModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLASavePinBillCell *cell = [tableView dequeueReusableCellWithIdentifier:kBillCellID forIndexPath:indexPath];
    MLASavePinBillModel *model = modelsArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MLASavePinBillModel *model = modelsArr[indexPath.row];
    MLASavePinBillDetailVC *detailVC = [[MLASavePinBillDetailVC alloc] init];
    detailVC.modelId = model.modelId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - Actions
- (void)rightItemAction {
    CX_Log(@"添加票据凭证");
    [self.view endEditing:YES];
    MLASavePinBillAddVC *addVC = [[MLASavePinBillAddVC alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
    @weakify(self);
    addVC.addBillSuccessBlock = ^{
        @strongify(self);
        [self loadData];
    };
}

- (void)addClickActions {
    @weakify(self);
    searchView.editChangeBlock = ^(NSString * _Nonnull keyword) {
        @strongify(self);
        self->keyword = keyword;
        [self loadData];
    };
}

@end
